package com.unhz.taigu.controller;

import com.unhz.generator.pojo.Logistics;
import com.unhz.generator.pojo.ProductionOrder;
import com.unhz.generator.utils.EmptyUtil;
import com.unhz.generator.utils.JsonResult;
import com.unhz.generator.utils.Page;
import com.unhz.generator.utils.PageSize;
import com.unhz.taigu.service.DeliveryOrderService;
import com.unhz.taigu.service.LogisticsService;
import com.unhz.taigu.service.ProductionOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @Author: 曹显贵
 * @Description: 排产订单控制层
 * @Date: Created in 10:17 2018/1/30 0030
 * @Modified By:
 */
@Controller
@RequestMapping("/productionOrder")
public class ProductionOrderController {

    private final Logger logger = LoggerFactory.getLogger(ProductionOrderController.class);

    @Autowired
    private ProductionOrderService productionOrderService;
    //引入物流的service
    @Autowired
    private LogisticsService logisticsService;
    @Autowired
    private DeliveryOrderService deliveryOrderService;

    /**
     * 分页查询订单
     * @param model
     * @param pageNo
     * @param keywords
     * @param request
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping("/findAllProductionOrder")
    public String findPageAllProductionOrder(Model model,
                                             @RequestParam(value = "pageNo",defaultValue = "1",required = false)String pageNo,
                                             @RequestParam(value = "keywords",defaultValue = "",required = false)String keywords,
                                             @RequestParam(value = "adminId",defaultValue = "",required = false)Integer adminId,
                                             @RequestParam(value = "roleId",defaultValue = "",required = false)Integer roleId,
                                             HttpServletRequest request) throws UnsupportedEncodingException {
        if("GET".equals(request.getMethod())){
            keywords = new String(keywords.getBytes("iso-8859-1"),"utf-8");
            logger.info("转换格式");
        }
        Page<ProductionOrder> page = productionOrderService.findPageAllProductionOrder(pageNo, PageSize.pageSize,keywords,adminId,roleId);
        model.addAttribute("page",page);
        model.addAttribute("keywords",keywords);
        return "production/index.jsp";
    }

    /**
     * 跳转到修改页面
     * @param productOrderId
     * @param model
     * @return
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(@RequestParam("productOrderId")Integer productOrderId,Model model){
        ProductionOrder productionOrder = productionOrderService.selectByProductOrderId(productOrderId);
        model.addAttribute("productionOrder",productionOrder);
        return "production/update.jsp";
    }

    /**
     * 修改排产信息
     * @param productionOrder
     * @return
     */
    @RequestMapping("/Update")
    public String updateProduction(ProductionOrder productionOrder) {
        return "redirect:findAllProductionOrder";
    }

    /**
     * 确认排产订单
     * @param productOrderId
     * @return
     */
    @RequestMapping("/confirm")
    @ResponseBody
    public JsonResult confirmProductOrder(@RequestParam("productOrderId")Integer productOrderId){

        String index = productionOrderService.confirmProductOrder(productOrderId);
        if("success".equals(index)){
            return JsonResult.success("确认排产订单成功");
        }
        return JsonResult.failure("确认排产订单失败");
    }

    /**
     * 进行生产
     * @param productOrderId
     * @return
     */
    @RequestMapping("/carryOutProduct")
    @ResponseBody
    public JsonResult carryOutProduct(@RequestParam("productOrderId")Integer productOrderId){
        String index = productionOrderService.carryOutProduct(productOrderId);
        if("success".equals(index)){
            return JsonResult.success("确认排产订单成功");
        }
        return JsonResult.failure("确认排产订单失败");
    }

    /**
     * 删除排产订单
     * @param productOrderId
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(@RequestParam("productOrderId")Integer productOrderId){
        int count= productionOrderService.deleteByProducOrderId(productOrderId);
        if(count > 0)
        {
            return JsonResult.success("删除排产订单成功");
        }
        return JsonResult.failure("删除排产订单失败!");
    }
}
